package game.map;

import game.map.mapEvents.MapEvent;

import java.util.List;

import clientApp.utils.RPG_Workstation_Container;

import communication.client.PlayerInteractionManager;
import communication.server.MasterInteractionManager;

import processing.core.*;

public class WsApplet extends PApplet {

	private static final long serialVersionUID = 1L;

	private MapInteraction interact;

	protected boolean cache = false;

	private boolean isPlayer;
	private PlayerInteractionManager player = null;
	private MasterInteractionManager master = null;

	public WsApplet(RPG_Workstation_Container container, boolean isPlayer) {
		this.isPlayer = isPlayer;
		if (isPlayer)
			this.player = container.player;
		else
			this.master = container.master;
	}

	public PlayerInteractionManager getPlayer() {
		return player;
	}

	public MasterInteractionManager getMaster() {
		return master;
	}

	@Override
	public void setup() {

		// tm = new TiledMap(4, 2, 5, 600, 600, this);
		// tm = new TiledMapInGame(5, 5, 5, 600, 800, this);
		// interact = new MapInteraction(tm, this);

		size(600, 600);
		smooth();

	}

	@Override
	public void draw() {
		background(0);
		interact.translate();
		interact.draw();
		cache = true;
	}

	@Override
	public void keyPressed() {
		interact.keyPressed(key);
		cache = false;
	}

	@Override
	public void keyReleased() {
		interact.keyReleased();
		cache = false;
	}

	@Override
	public void mousePressed() {
		if (interact.isYourTurnMouse()) {
			interact.mousePressed();
			cache = false;
		}
	}

	@Override
	public void mouseReleased() {
		if (interact.isYourTurnMouse()) {
			interact.mouseReleased();
			cache = false;
		}
	}

	public void loadPlayersMap(String path, List<MapUnit> units, List<MapItem> itens, List<MapEvent> events, long myId) {

		TiledMapInGame tm = (isPlayer) ? player.getTiledMap() : master.getTiledMap();
		tm.load(path, units); // only heroes should be passed on, monsters are
								// on the map
		for (MapUnit mu : units)
			mu.setPosition(tm.getInitX(), tm.getInitY());
		tm.putItens(itens);
		tm.putEvents(events);

		if (isPlayer) {
			interact = new MapInteraction(tm, this);
			interact.setWhoIAm(myId);
		} else {
			interact = new MapInteractionServer(tm, this);
		}

		tm.setActiveUnits();
	}

	static public void wsInit() {
		PApplet.main(new String[] { "--bgcolor=#F0F0F0", "game.map.WsApplet" });
	}

	public MapInteraction getMapInteraction() {
		return interact;
	}
	
	public MapInteractionServer getMapInteractionServer() {
		return (MapInteractionServer)interact;
	}

	/**
	 * Width e height fixos para encaixar na imagem de fundo.
	 */
	@Override
	public int getWidth() {
		return 600;
	}

	@Override
	public int getHeight() {
		return 600;
	}

}
